<template>
  <div class="permission">
    <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="id" border>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code" align="center"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #="{ row, $index }">
          <!-- 级别level为4的菜单不允许添加菜单 -->
          <el-button type="primary" size="small" @click="addPermission(row)"
            :disabled="row.level == 4 ? true : false">{{ row.level == 3 ? '添加功能' : '添加菜单' }}</el-button>
          <!-- 级别level为1的菜单不允许编辑和删除 -->
          <el-button type="primary" size="small" @click="updatePermission(row)"
            :disabled="row.level == 1 ? true : false">编辑</el-button>
          <el-popconfirm :title="`你确定要删除${row.name}?`" width="260px" @confirm="deletePermission(row.id)">
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"
                :disabled="row.level == 1 ? true : false">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="showPermissionDialog" :title="menuData.id ? '更新菜单' : '添加菜单'">
      <el-form label-width="80px">
        <el-form-item label="菜单名称">
          <el-input placeholder="请输入菜单名称" v-model="menuData.name"></el-input>
        </el-form-item>
        <el-form-item label="权限值">
          <el-input placeholder="请输入菜单权限值" v-model="menuData.code"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang='ts'>
import { onMounted, ref, reactive } from 'vue'
import { getAllPermissionAPI, addOrUpdateMenu, removeMenu } from '@/api/acl/menu/index.ts'
import { ElMessage } from 'element-plus';
const tableData = ref([])
// 添加菜单和编辑弹窗dialog显示与隐藏
let showPermissionDialog = ref(false)
//携带的参数
let menuData = reactive({
  code: "",
  level: 0,
  name: "",
  pid: 0,
})
// 获取全部菜单与按钮标识的数据
const getAllPermission = async () => {
  let result = await getAllPermissionAPI()
  const { code, data } = result
  if (code == 200) {
    tableData.value = data
  }
}
// 添加菜单权限
const addPermission = (row) => {
  //清空数据
  Object.assign(menuData, {
    id: 0,
    code: "",
    level: 0,
    name: "",
    pid: 0,
  })
  //收集新增的菜单的level数值
  menuData.level = row.level + 1;
  //给谁新增子菜单
  menuData.pid = (row.id as number);
  showPermissionDialog.value = true
}
// 编辑菜单
const updatePermission = (row) => {
  console.log(row);
  Object.assign(menuData, row)
  showPermissionDialog.value = true
}
// 删除菜单
const deletePermission = async (id: number) => {
  let result = await removeMenu(id)
  const { code } = result
  if (code == 200) {
    ElMessage.success('删除菜单成功')
    // 重新获取列表页数据
    getAllPermission()
  } else {
    ElMessage.error('删除菜单失败')
  }
}
// dialog的取消事件
const cancel = () => {
  console.log('cancel');
  showPermissionDialog.value = false

}
// dialog的确定事件
const confirm = async () => {
  // 整理传参数据
  let result = await addOrUpdateMenu(menuData)
  const { code } = result
  if (code == 200) {
    ElMessage.success(menuData.id ? '添加菜单成功' : '修改菜单成功')
    // 重新获取列表数据
    getAllPermission()
  } else {
    ElMessage.error(menuData.id ? '添加菜单失败' : '修改菜单失败')
  }
  showPermissionDialog.value = false
}
onMounted(() => {
  getAllPermission()
})

</script>
<style lang="scss" scoped></style>